package lk.icta.core.service.impl;

import lk.icta.core.dao.AlienationMethodDAO;
import lk.icta.core.entity.AlienationMethod;
import lk.icta.core.service.AlienationMethodsManagementService;

import lk.icta.common.ELandRuntimeException;
import lk.icta.common.ErrorCodes;
import lk.icta.common.service.MailSenderService;
import lk.icta.core.dao.ScheduleDAO;
import lk.icta.core.entity.Schedule;
import lk.icta.core.service.ObjectionManagementService;
import lk.icta.core.service.ScheduleManagementService;
import lk.icta.user.common.Permission;
import lk.icta.user.entity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


/**
 @author Sisiruwan Senadeera
 */
public class AlienationMethodsManagementServiceImpl implements AlienationMethodsManagementService{

    private static final Logger logger = LoggerFactory.getLogger(AlienationMethodsManagementServiceImpl.class);
    private final AlienationMethodDAO alienationMethodDAO;

    public AlienationMethodsManagementServiceImpl(AlienationMethodDAO alienationMethodDAO){
         this.alienationMethodDAO=alienationMethodDAO;
    }

     /**
     * Adding an AlienationMethod
     *
     * @param alienationMethod The AlinationMethod object to be added
     */

    @Transactional(propagation = Propagation.REQUIRED)
    public void addAlienationMethod(AlienationMethod alienationMethod,User user){
        logger.debug("Adding a objection {}", alienationMethod.getId());
        alienationMethodDAO.addAlienationMethod(alienationMethod,user);
    }

    /**
        * To get all the updatable Alienation Methods according to the language
    */
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<AlienationMethod> getAllUpdatableAlienationMethods(){
        return alienationMethodDAO.getAllUpdatableAlienationMethods();
    }

    /**
     * Return an AlienationMethod corresponding to the given id
     *
     * @param id   alienation method id
     * @param user user requesting the operation
     * @return AlienationMethod
     */

    @Transactional(propagation = Propagation.SUPPORTS)
    public AlienationMethod getAlienationMethodById(int id, User user){
        return alienationMethodDAO.getAlienationMethodById(id,user);
    }

    /**
     * Updating an existing Alienation Method
     *
     * @param alienationMethod  The AlienationMethod to be updated.
     */
    @Transactional(propagation = Propagation.REQUIRED)
     public void updateAlienationMethod(AlienationMethod alienationMethod,User user){
        AlienationMethod existing=alienationMethodDAO.getAlienationMethodById(alienationMethod.getId(),user);
        alienationMethod.getLifeCycleInfo().setCreatedTimeStamp(existing.getLifeCycleInfo().getCreatedTimeStamp());
        alienationMethod.getLifeCycleInfo().setCreatedUser(existing.getLifeCycleInfo().getCreatedUser());
        alienationMethodDAO.updateAlienationMethod(alienationMethod,user);
     }

}
